//
//  PushWebViewVC.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "PushWebViewVC.h"
#import "BlackManager.h"
#import "SetBlack.h"
#import "NewsDataBaseManager.h"
#import "LoginViewController.h"
@interface PushWebViewVC ()
@property (nonatomic, retain)UIButton *likeBtn;
@property (nonatomic, assign)NSInteger i;//判断是否存在数据库中
@property (nonatomic, assign)NSInteger mId;
@end

@implementation PushWebViewVC
- (void)dealloc
{
    [_pathstr release];
    [_pathhtml release];
    [_model release];
    [_likeBtn release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-newlisticon06"] style:UIBarButtonItemStylePlain target:self action:@selector(BackAction)] autorelease];
    
    self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _likeBtn.frame = CGRectMake(0, 0, 30, 30);
    //设置收藏标志
    NewsDataBaseManager *newsDb = [NewsDataBaseManager shareInstance];
    [newsDb openDB];
    NSMutableArray *arr = [newsDb select];
    self.i = 0;
    for (AllDetailModel *model in arr) {
        if ([_model.title isEqualToString:model.title]) {
            _i = 1;
        }
    }
    if (_i == 1) {
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"news"];
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
    }
    if (_i == 0) {
        [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"news"];
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
    }
    //收藏方法
    [_likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_likeBtn];
    
    
    //webView
    UIWebView *webVC = [[UIWebView alloc]initWithFrame:CGRectMake(0, -62, self.view.frame.size.width, self.view.frame.size.height + 62)];
    webVC.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webVC];
    if ([BlackManager sharedInstance].isNight) {
        [webVC stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.background=‘#F6F7F3‘"];
    }
    //webView适应
    webVC.scalesPageToFit = YES;
    if (_pathstr.length != 0) {
        [webVC loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_pathstr]]];
    }else{
        
        [webVC loadHTMLString:_pathhtml baseURL:[NSURL URLWithString:_pathhtml]];
    }
    [webVC release];
    
}
-(void)likeAction:(UIButton *)sender
{
    NSLog(@"收藏");
    BOOL login = [[NSUserDefaults standardUserDefaults]boolForKey:@"register"];
    if (login) {
        //打开数据库
        NewsDataBaseManager *dbManager = [NewsDataBaseManager shareInstance];
        [dbManager openDB];
        //创建表
        [dbManager createTable];
        //如果已经收藏了 点击按钮
        if (_i) {
            if (!_likeBtn.selected) {
                //取消收藏
                NSMutableArray *arr = [[NewsDataBaseManager shareInstance]select];
                //获取当前数据在数据库中的位置
                for (AllDetailModel *m in arr) {
                    if ([m.title isEqualToString:_model.title]) {
                        _mId = m.NewsId;
                    }
                }
                [dbManager deleteNewsWithId:_mId];
                [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
                
                NSLog(@"取消了收藏");
                //新闻收藏标志
                [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"news"];
            }
        }else if (!_likeBtn.selected) {
            //收藏
            NSString *s = _model.title;
            //新闻收藏标志
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"news"];
            
            //插入数据
            [dbManager insertNews:_model];
            [_likeBtn setBackgroundImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
            
            NSLog(@"喜欢了1%@", s);
        }
        _likeBtn.selected = !_likeBtn.selected;
    }else{
        LoginViewController *login = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:login animated:YES];
        [login release];
    }

    
}
-(void)BackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
